<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <title>jhipsterControlCenter - Swagger UI</title>
    <link rel="stylesheet" type="text/css" href="./swagger-ui.css" />
    <link rel="icon" type="image/png" href="./favicon-32x32.png" sizes="32x32" />
    <link rel="icon" type="image/png" href="./favicon-16x16.png" sizes="16x16" />
  </head>

  <body>
    <div id="swagger-ui"></div>

    <script src="./swagger-ui-bundle.js"></script>
    <script src="./swagger-ui-standalone-preset.js"></script>
    <script src="./axios.min.js"></script>

    <script type="text/javascript">
      function getCSRF() {
        var name = 'XSRF-TOKEN=';
        var ca = document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
          var c = ca[i];
          while (c.charAt(0) === ' ') c = c.substring(1);
          if (c.indexOf(name) !== -1) return c.substring(name.length, c.length);
        }
        return '';
      }

      const AlwaysEnableTryItOutPlugin = function (system) {
        const OperationContainer = system.getComponents('OperationContainer');
        return {
          components: {
            TryItOutButton: () => null,
            OperationContainer: class CustomOperationContainer extends OperationContainer {
              constructor(...args) {
                super(...args);
                this.state.tryItOutEnabled = true;
              }
            },
          },
        };
      };

      window.onload = function () {
        // Automatically click all 'Try it out' buttons
        var mutationObserver = new MutationObserver(function (mutations) {
          mutations.forEach(function (mutation) {
            document.querySelectorAll('.btn.try-out__btn:not(.cancel)').forEach(div => {
              div.click();
            });
          });
        });

        mutationObserver.observe(document.documentElement, {
          attributes: true,
          childList: true,
          subtree: true,
        });

        var urls = [];
        axios.get('/swagger-resources').then(function (response) {
          response.data.forEach(function (resource) {
            urls.push({ name: resource.name, url: resource.location });
          });

          urls.sort(function (a, b) {
            var x = a.name.toLowerCase(),
              y = b.name.toLowerCase();
            return x < y ? -1 : x > y ? 1 : 0;
          });

          // Build a system
          var ui = SwaggerUIBundle({
            urls: urls,
            dom_id: '#swagger-ui',
            deepLinking: true,
            filter: true,
            layout: 'StandaloneLayout',
            withCredentials: true,
            presets: [SwaggerUIBundle.presets.apis, SwaggerUIStandalonePreset],
            plugins: [SwaggerUIBundle.plugins.DownloadUrl, AlwaysEnableTryItOutPlugin],
            requestInterceptor: function (req) {
              return axios.get('/management/info').then(function (response) {
                var oauth2 = false;
                response.data.activeProfiles.forEach(function (activeProfile) {
                  if (activeProfile === 'oauth2') {
                    oauth2 = true;
                  }
                });
                // Remove the sample Swagger UI request body if present
                if (
                  req.method === 'GET' &&
                  req.body === '{"additionalProp1":"string","additionalProp2":"string","additionalProp3":"string"}'
                ) {
                  req.body = undefined;
                }
                if (oauth2) {
                  // OAuth2
                  req.headers['X-XSRF-TOKEN'] = getCSRF();
                } else {
                  // working only with JWT authentication
                  var authToken = localStorage.getItem('jhi-authenticationToken') || sessionStorage.getItem('jhi-authenticationToken');
                  if (authToken) {
                    req.headers['Authorization'] = 'Bearer ' + authToken;
                  }
                  // Remove the sample Swagger UI request body if present
                  if (
                    req.method === 'GET' &&
                    req.body === '{"additionalProp1":"string","additionalProp2":"string","additionalProp3":"string"}'
                  ) {
                    req.body = undefined;
                  }
                }
                return req;
              });
            },
          });

          window.ui = ui;
        });
      };
    </script>
  </body>
</html>
